﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolERP_BLL;
using CoolERP_Entities;
using CoolERP_Common;

namespace CoolERP_Web
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                UpdatePath(null);            
        }

        /// <summary>
        /// 更新PATH，直接调用：UpdatePath(null)
        /// </summary>
        /// <param name="Pid"></param>
        private void UpdatePath(Guid? Pid)
        {
            using (DepartmentsBLL dBLL = new DepartmentsBLL())
            {
                List<Departments> depList = dBLL.GetAll();
                foreach (var item in depList.Where(t => t.ParentId == Pid))
                {
                    if (Pid == null)
                        item.Path = item.Id.ToString();
                    else
                    {
                        Departments parent = depList.FirstOrDefault(t => t.Id == item.ParentId);
                        item.Path = string.Format("{0},{1}", parent.Path, item.Id);
                    }
                    dBLL.Update(item);
                    UpdatePath(item.Id);
                }
            }
        }
    }
}
